<?php
/**
 * 员工
 */
apf_require_controller("HrAbstract");

class Hr_Staff_DimissionController extends HrAbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        if(empty($params['user_id'])){
            $this->show_tips('ERROR_NO_SELECT');
        }
        $uid = $params['user_id'];
        $user_bll = new Bll_Hr_User();
        $bll = new Bll_Hr_Property();
        $re_bll = new Bll_Hr_UserRelation();
        //员工信息
        $data['user_data'] = $user_bll->get_user_by_id($uid);

        if(empty($data['user_data'])){
            $this->show_tips('ERROR_NO_SELECT');
        }
        //get work
        $job_info = $user_bll->get_user_work_dep($uid);
        if(empty($job_info)){
            $this->show_tips('ERROR_NO_SELECT');
        }
        $lower_num = $re_bll->get_user_sub_count($uid, $job_info['job_id']);

        if(!empty($_POST) && !empty($params['lastDay']) && !empty($params['dimissionDay'])){
            $params['lastDay'] = str_replace('-','',$params['lastDay']);
            $params['dimissionDay'] = str_replace('-','',$params['dimissionDay']);
            if($params['lastDay'] > date('Ymd') || $params['dimissionDay'] > date('Ymd')){
                $this->show_tips('ERROR_USER_DIMISSION_DATE');
            }
            if($lower_num){
                $this->show_tips('ERROR_USER_LOWER_NUM');
            }
            $updata = $this->_build_update_data($params);
            $user_bll->dimission_user($uid,$updata);
            $gourl = Util_BaseUrls::staff_list_url();
            $this->show_tips('OPT_SUCCESS',$gourl,1);
        }

        $data['job_info'] = $job_info;

        //离职类型
        $data['leave_type'] = $bll->get_dimission_reasion();
        $data['leave_active'] = $bll->get_active_reason();
        $data['leave_passive'] = $bll->get_passive_reason();
        $data['leave_no_arrive'] =array();
        //pm
        $data['p_leave'] = $bll->get_p_degree();
        $data['m_leave'] = $bll->get_m_degree();

        $data['sub_url'] = Util_BaseUrls::build_search_url('parent_id', $uid);
        //下级人数
        $data['lower_num'] = $lower_num;
        $data['user_id'] = $params['user_id'];
        $this->request->set_attribute('data', $data);
        return "Hr_Staff_Dimission";
    }

    private function _build_update_data($params){
        $updata = array(
                'leaving_type'=>$params['optionsRadios'],
                'leaving_date'=>str_replace('-','',$params['dimissionDay']),
                'last_date'=>str_replace('-','',$params['lastDay']),
        );
        $updata['reason_id']=0;
        $updata['other_reason']='';
        if($params['optionsRadios']==1){
            $updata['reason_id']=$params['reason1'];
            if($params['reason1']==14){
                $updata['other_reason'] = $params['otherReason'];
            }
        }elseif($params['optionsRadios']==2){
            $updata['reason_id'] = $params['reason2'];
        }
        return $updata;
    }
}